/**
 * 用户关注列表
 */

import $http from '@/http';

const State = {
  uids: [],
  brandNames: [],
  numberIds: []
};

// getters
const Getters = {
  uids: state => state.uids,
  brandNames: state => state.brandNames,
  numberIds: state => state.numberIds
};

// mutations
const Mutations = {
  reset: state => {
    const s = {
      uids: [],
      brandNames: [],
      numberIds: []
    };
    Object.keys(s).forEach(key => {
      state[key] = s[key];
    });
  },
  SET_UIDS: (state, uids) => {
    state.uids = [...uids];
  },
  SET_BRANDNAMES: (state, brandNames) => {
    state.brandNames = [...brandNames];
  },
  SET_NUMBERIDS: (state, numberIds) => {
    state.numberIds = [...numberIds];
  }
};
// actions
const Actions = {
  getUids({ commit }) {
    return new Promise((resolve, reject) => {
      $http
        .post({ map: 'SHOP', url: 'getFocusUids' })
        .then(res => {
          const { data } = res;
          if (data['success']) {
            commit('SET_UIDS', data['data']);
            resolve(data);
          } else {
            reject(data);
          }
        })
        .catch(err => {
          console.error('my error');
          reject(err);
        });
    });
  },
  getBrandNames({ commit }) {
    return new Promise((resolve, reject) => {
      $http
        .post({ map: 'BRAND', url: 'getFocusBrandNames' })
        .then(({ data }) => {
          if (data['success']) {
            commit('SET_BRANDNAMES', data['data']);
            resolve(data);
          } else {
            reject(data);
          }
        })
        .catch(err => {
          reject(err);
        });
    });
  },
  getNumberIds({ commit }) {
    return new Promise((resolve, reject) => {
      $http
        .post({ map: 'PRODUCT', url: 'getFocusNumberIds' })
        .then(({ data }) => {
          if (data['success']) {
            commit('SET_NUMBERIDS', data['data']);
            resolve(data);
          } else {
            reject(data);
          }
        })
        .catch(err => {
          reject(err);
        });
    });
  }
};

export default {
  namespaced: true,
  strict: true,
  state: State,
  getters: Getters,
  actions: Actions,
  mutations: Mutations
};
